package com.marvels.approve.loan.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.approve.common.dto.InsurancePolicyDTO;
import com.marvels.approve.common.utils.InsuranceRateUtil;
import com.marvels.approve.loan.dao.McamLoanInsurancePolicyInfoMapper;
import com.marvels.approve.loan.dto.McamAcLoanApplyBdInfo;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.service.McamLoanInsurancePolicyInfoService;
import com.marvels.approve.process.dao.TaskProcessDao;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;

@Service
public class McamLoanInsurancePolicyInfoServiceImpl implements McamLoanInsurancePolicyInfoService {
	@Autowired
	private McamLoanInsurancePolicyInfoMapper mcamLoanInsurancePolicyInfoMapper;
	@Autowired
	private TaskProcessDao taskProcessMapper;
	@Override
	public int saveAssureBx(McamLoanApplyInfoDTO loan,boolean isNeeedQueryCache) throws MarvelsBusinessException {
		int count = 0;
		try {
			String userCode = MarvelsConstant.USER;
			if(isNeeedQueryCache){
				UserDTO user = CacheUtils.getCacheUser();
				if(user!=null){
					userCode = user.getUserCode();
				}
			}
			//生成祁安保险
			McamAcLoanApplyBdInfo insurancePolicyDto = new McamAcLoanApplyBdInfo();
			insurancePolicyDto.setInsuranceAmount(InsuranceRateUtil.getBxMoney(loan));
			insurancePolicyDto.setInsuranceId(System.currentTimeMillis() + "" + new Random().nextInt(999999));
			insurancePolicyDto.setLoanId(loan.getLoanId());
			insurancePolicyDto.setInsuranceCount(1);
			insurancePolicyDto.setInsuranceCpyName("祁安保险");
			insurancePolicyDto.setInsuranceType("3225010001");
			insurancePolicyDto.setInsuranceCpyCode("qabx");
			insurancePolicyDto.setCreatedBy(userCode);
			insurancePolicyDto.setUpdatedBy(userCode);
			count = mcamLoanInsurancePolicyInfoMapper.insertInsurancePolicy(insurancePolicyDto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("生成保单异常！！！"+e);
			throw new MarvelsBusinessException("生成保单异常");
		}
		return count;
	}
	@Override
	public int updateAssureBx(McamAcLoanApplyBdInfo insurancePolicyDto) throws MarvelsBusinessException {
		if(StringUtils.isEmpty(insurancePolicyDto.getUpdatedBy())){
			insurancePolicyDto.setUpdatedBy(MarvelsConstant.USER);
		}
		return mcamLoanInsurancePolicyInfoMapper.updateInsuranceAmt(insurancePolicyDto);
	}
	@Override
	public InsurancePolicyDTO selectInsuranceByLoanIdAndCompany(String loanId, String companyCode) throws MarvelsBusinessException {
		return mcamLoanInsurancePolicyInfoMapper.selectInsuranceByLoanIdAndCompany(loanId, companyCode);
	}
	@Override
	public List<InsurancePolicyDTO> selectInsuranceByLoanId(String loanId) throws MarvelsBusinessException {
		return mcamLoanInsurancePolicyInfoMapper.selectInsuranceByLoanId(loanId);
	}
	/**
	 *保单信息
	 */
	@Override
	public List<InsurancePolicyDTO> getBdInfoList(InsurancePolicyDTO loanApplyInfoDto) throws MarvelsBusinessException {
		if(loanApplyInfoDto==null) throw new MarvelsBusinessException("入参为空");
		MarvelsLogUtil.getInstance().info("同步保单入参=======》》》》》:" + loanApplyInfoDto.toString());
		List<InsurancePolicyDTO> loanBdInfoList = this.selectInsuranceByLoanId(loanApplyInfoDto.getLoanId());
		MarvelsLogUtil.getInstance().info("同步保单出参=======》》》》》:" + loanBdInfoList);
		return loanBdInfoList;
	}
	/**
	 * 生成保单
	 */
	@Override
	public int generateBdInfo(McamLoanApplyInfoDTO loanApplyInfoDto) throws MarvelsBusinessException {
		if(loanApplyInfoDto==null) throw new MarvelsBusinessException("入参为空");
		MarvelsLogUtil.getInstance().info("生成保单入参=======》》》》》:" + loanApplyInfoDto.toString());
		int count = 0;
		//校验参数
		if(StringUtils.isEmpty(loanApplyInfoDto.getLoanId())){
			throw new MarvelsBusinessException("进件ID为空");
		}
		if(loanApplyInfoDto.getPrincipalAmtAct()==null){
			throw new MarvelsBusinessException("核定金额为空");
		}
		if(StringUtils.isEmpty(loanApplyInfoDto.getProductType())){
			throw new MarvelsBusinessException("产品大类为空");
		}
		if(loanApplyInfoDto.getApplyTerms()==null){
			throw new MarvelsBusinessException("申请期数为空");
		}
		//判断核定金额是否存在
		McamLoanApplyInfoDTO mcamLoanApplyInfoDto = taskProcessMapper.selectLoanApplyInfo(loanApplyInfoDto.getLoanId());
		BigDecimal principalAmtAct = null;
		if(mcamLoanApplyInfoDto!=null){
			//核定金额
			principalAmtAct = mcamLoanApplyInfoDto.getPrincipalAmtAct();
		}
		//判断是否已经存在保单
		InsurancePolicyDTO bxDto = this.selectInsuranceByLoanIdAndCompany(loanApplyInfoDto.getLoanId(), "qabx");
		if(principalAmtAct!=null){
			loanApplyInfoDto.setPrincipalAmtAct(principalAmtAct);
		}
		if(bxDto==null){//新增
			count = this.saveAssureBx(loanApplyInfoDto,false);
		}else{//更新
			try {
				McamAcLoanApplyBdInfo insurancePolicyDto = new McamAcLoanApplyBdInfo();
				insurancePolicyDto.setLoanId(loanApplyInfoDto.getLoanId());
				insurancePolicyDto.setInsuranceAmount(InsuranceRateUtil.getBxMoney(loanApplyInfoDto));
				count = this.updateAssureBx(insurancePolicyDto);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error("更新保单异常！！！"+e);
				throw new MarvelsBusinessException("更新保单异常");
			}
		}
		if(count<=0) throw new MarvelsBusinessException("生成保单失败======》》》》》》》："+count);
		return count;
	}
	/**
	 * 修改保单密码
	 */
	@Override
	public Integer modifyBdPassword(InsurancePolicyDTO loanApplyInfoDto) throws MarvelsBusinessException {
		if(loanApplyInfoDto==null) throw new MarvelsBusinessException("入参为空");
		MarvelsLogUtil.getInstance().info("修改保单密码入参=======》》》》》:" + loanApplyInfoDto.toString());
		if(StringUtils.isEmpty(loanApplyInfoDto.getLoanId())){
			throw new MarvelsBusinessException("进件ID为空");
		}
		if(StringUtils.isEmpty(loanApplyInfoDto.getInsuranceCpyCode())){
			throw new MarvelsBusinessException("保单公司ID");
		}
		if(StringUtils.isEmpty(loanApplyInfoDto.getInsuranceAccountPwd())){
			throw new MarvelsBusinessException("保单查询密码为空");
		}
		if(StringUtils.isEmpty(loanApplyInfoDto.getInsuranceAccountName())){
			throw new MarvelsBusinessException("保单查询账户为空");
		}
		if(StringUtils.isEmpty(loanApplyInfoDto.getUpdatedBy())){
			loanApplyInfoDto.setUpdatedBy(MarvelsConstant.USER);
		}
		loanApplyInfoDto.setPolicySrsFlag(1);
		Integer count = mcamLoanInsurancePolicyInfoMapper.modifyBdPassword(loanApplyInfoDto,2);
		if(count<=0) throw new MarvelsBusinessException("修改保单密码失败======》》》》》》》："+count);
		MarvelsLogUtil.getInstance().info("修改保单密码入出参=======》》》》》:" + count);
		return count;
	}

}
